package com.leyou.cart.test;

import org.apache.commons.lang3.RandomStringUtils;

public class ManyThreadDemo {

    private ThreadLocal<Integer> TL = new ThreadLocal<>();

    public void threadDemo(){
        for (int i=0; i<6; i++){
            //模拟六个线程
            new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        Integer userId = Integer.valueOf(RandomStringUtils.randomNumeric(3));
                        //把当前用户的id存入到map中
                        TL.set(userId);
                        System.out.println(Thread.currentThread().getName()+"【放入了】:"+userId);
                        //再去map中获取一次
                        System.out.println(Thread.currentThread().getName()+"第一次【取出了】:"+TL.get());
                        System.out.println(Thread.currentThread().getName()+"第二次【取出了】:"+TL.get());
                    }
                }
            ).start();
        }
    }

    public static void main(String[] args) {
        new ManyThreadDemo().threadDemo();
    }

}
